<template>
    <el-dialog :title="printTitle" :visible.sync="open" width="60%" append-to-body :before-close="closeDelDialog">
        <div style="overflow-y: auto;" :style="'height:' + tableHeight + 'px'">
        <div class="printMain" v-if="printShow">
            <div style="margin-left:20px;">
                <el-button type="danger" plain size="medium" @click="printTap">打印</el-button>
                <el-button @click="closeDelDialog">取 消</el-button>
            </div>
        </div>
         <!-- istype=1  添加登记单    istype=2 打印登记单 -->
         <transferComponent istype="2" ref="childComponent" :form="form"></transferComponent>
          <!--审批相关-->
          <div v-if="!printShow" style=" margin:20px 15%; ">
            <el-form  ref="otherForm" :model="otherForm" :rules="otherRules" label-width="120px">
              <el-row>
                <el-col :span="18">
                  <el-form-item label="审批意见：">
                    <el-radio-group v-model="otherForm.resource" :disabled="isType == '3'">
                      <el-radio :label="1">同意</el-radio>
                      <el-radio :label="0">不同意</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="18" >
                  <el-form-item label="批 注：">
                    <el-input v-model="otherForm.note" type="textarea" :rows="3" :disabled="isType == '3'"></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer" style="text-align: center">

              <el-button type="primary" @click="submit" :disabled="isType == '3'">确 定</el-button>
              <el-button @click="closeDelDialog">取 消</el-button>
            </div>
          </div>
        </div>
    </el-dialog>
</template>

<script>
import { selectHandOverReceiveById, selectHandOverReceiveById1 } from '@/api/archive/archiveTransfer/archiveTransfer.js'
import transferComponent from "@/views/archive/archiveTransfer/transferComponent.vue";//移交登记单， 登记和查看详情用

import estimateTableHeight from '@/mixins/estimateTableHeight';
import {
  getOnlineReceptionById
} from "../../../../api/archive/digitalizeReceive/electronicArchives/onlineReceivingRecords";
export default {
    props: ["open", 'printRow', 'source', 'isType','printShow','printTitle'],
    mixins: [estimateTableHeight], // 混入方法
    components:{
        transferComponent
    },
    watch: {
        printRow: {
            handler(valNew) {
                this.selectHandOverPost();
            },
            immediate: true
        }
    },
    data() {
        return {
            form: {},
            //在线onLine 离线outLine
            type: {
                onLine: "onLine ",
                outLine: "outLine"
            },
          printShow:true,
          printTitle:"打印移交接收登记表",
          otherForm: {
            resource: undefined,
            note: undefined
          },
          otherRules: {
            resource: [
              { required: true, message: "审批意见不能为空", trigger: "blur" }
            ]
          },
        }
    },
    methods: {
        selectHandOverPost() {
          if(this.printRow != undefined&&this.printRow!= null&&this.printRow!= "") {
            console.log(this.source,"this.source")
            if (this.source == 'offline') {

              selectHandOverReceiveById1({id: this.printRow.id}).then(res => {
                this.form = res.data;
                console.log(res.data)
                const writtenTime = res.data.writtenTime.match(/\d+/g);
                this.form.writtenTimeYear = writtenTime[0];
                this.form.writtenTimeMouth = writtenTime[1];
                this.form.writtenTimeData = writtenTime[2];
                const auditorTime = res.data.auditorTime.match(/\d+/g);
                this.form.auditorTimeYear = auditorTime[0];
                this.form.auditorTimeMouth = auditorTime[1];
                this.form.auditorTimeData = auditorTime[2];
                const unitTime = res.data.unitTime.match(/\d+/g);
                this.form.unitTimeYear = unitTime[0];
                this.form.unitTimeMouth = unitTime[1];
                this.form.unitTimeData = unitTime[2];
              })
            } else if (this.source == 'receivingApprovalRecords') {
              if (this.printRow.otherVariables) {
                this.otherForm['note'] = this.printRow.otherVariables.activitList[0].comment.controlValue
                this.otherForm['resource'] = this.printRow.otherVariables.activitList[0].isAgree.controlValue=="不同意"?0:1
              }
              getOnlineReceptionById({id: this.printRow.id}).then(res=>{
                if (res.data.type == 'outLine') {
                  this.form = res.data
                  const writtenTime = res.data.writtenTime.match(/\d+/g);
                  this.form.writtenTimeYear = writtenTime[0];
                  this.form.writtenTimeMouth = writtenTime[1];
                  this.form.writtenTimeData = writtenTime[2];
                  const auditorTime = res.data.auditorTime.match(/\d+/g);
                  this.form.auditorTimeYear = auditorTime[0];
                  this.form.auditorTimeMouth = auditorTime[1];
                  this.form.auditorTimeData = auditorTime[2];
                  const unitTime = res.data.unitTime.match(/\d+/g);
                  this.form.unitTimeYear = unitTime[0];
                  this.form.unitTimeMouth = unitTime[1];
                  this.form.unitTimeData = unitTime[2];
                } else {
                  this.form = res.data;
                  const writtenTime = res.data.writtenTime.match(/\d+/g);
                  this.form.writtenTimeYear = writtenTime[0];
                  this.form.writtenTimeMouth = writtenTime[1];
                  this.form.writtenTimeData = writtenTime[2];
                  const auditorTime = res.data.auditorTime.match(/\d+/g);
                  this.form.auditorTimeYear = auditorTime[0];
                  this.form.auditorTimeMouth = auditorTime[1];
                  this.form.auditorTimeData = auditorTime[2];
                  const unitTime = res.data.unitTime.match(/\d+/g);
                  this.form.unitTimeYear = unitTime[0];
                  this.form.unitTimeMouth = unitTime[1];
                  this.form.unitTimeData = unitTime[2];
                }
              })
            } else {
              if (this.printRow.otherVariables) {
                this.otherForm['note'] = this.printRow.otherVariables.activitList[0].comment.controlValue
                this.otherForm['resource'] = this.printRow.otherVariables.activitList[0].isAgree.controlValue=="不同意"?0:1
              }
              selectHandOverReceiveById({id: this.printRow.id}).then(res => {
                this.form = res.data;
                console.log(res.data)
                const writtenTime = res.data.writtenTime.match(/\d+/g);
                this.form.writtenTimeYear = writtenTime[0];
                this.form.writtenTimeMouth = writtenTime[1];
                this.form.writtenTimeData = writtenTime[2];
                const auditorTime = res.data.auditorTime.match(/\d+/g);
                this.form.auditorTimeYear = auditorTime[0];
                this.form.auditorTimeMouth = auditorTime[1];
                this.form.auditorTimeData = auditorTime[2];
                const unitTime = res.data.unitTime.match(/\d+/g);
                this.form.unitTimeYear = unitTime[0];
                this.form.unitTimeMouth = unitTime[1];
                this.form.unitTimeData = unitTime[2];
              })
            }
          }
        },

        submit(){
          if (this.otherForm.resource == undefined){
            this.$modal.msgWarning('请选择审批意见')
            return;
          }
          this.$parent.submitChild(this.otherForm);
          // this.reloadValPrint();
          // this.closeDelDialog();
          this.$emit("submitprint");
        },
        closeDelDialog() {
            this.otherForm = {};
            // this.reloadValPrint();
            this.$emit("closeDelDialog")
        },
        //重载打印的系统变量
        reloadValPrint(){
          this.printShow = true;
          this.printTitle = "打印移交接收登记表";
        },
        //打印
        printTap() {
            const tableElement = document.querySelector('.custom-table');
            const iframe = document.createElement('iframe');
            iframe.style.display = 'none';
            document.body.appendChild(iframe);
            const iframeDoc = iframe.contentWindow.document;

            //解决input输入框没有值的问题，需要引入jQuery

            if(this.form.type=="outLine"){
                $("input[name = outLine]").attr("checked",true)
            }else{
                $("input[name = onLine]").attr("checked",true)
            }
            const input = $('input');
            input.each(function () {
                $(this).attr('value', $(this).val());
            });
            //exportPdf为需要打印的div部分
            //获取到要打印的节点内容
            //   window.document.body.innerHTML = tableElement.innerHTML;//把打印内容赋值给页面内容
            // window.print();//调用打印方法
            // window.location.reload();//打印之后页面重新加载

            iframeDoc.write(`
          <html>
          <head>
            <style>
              @media print {

            /* 表格边框 */
            table {
            border-collapse: collapse;
            border: 1px solid black;
            }
            /* 表格内容边框 */
            td, th {
            text-align: center; /* 设置单元格文本居中 */
            height: 32px;
            line-height: 32px;
            border: rgb(42, 41, 41) 1px solid;
            padding:0 10px;
            font-size:12px
            }
                .custom-table {
                  width: 100%; /* 设置表格宽度为100% */
                  border-collapse: collapse; /* 合并单元格边框 */
                }
                .custom-cell {
                  padding:5px 5px 5px 5px;
                  align-items:center;
                  justify-content:flex-start;
                  opacity:1;
                  color:#000000;
                  letter-spacing:2px;
                  font-family:仿宋;
                  font-size:16px;
                  border: 1px solid #000; /* 设置单元格边框样式 */
                  padding: 8px; /* 设置单元格内边距 */
                  text-align: center; /* 设置单元格文本居中 */
                }
                .title {
                  heigth: 5em;
                  line-height: 5em;
                  text-align: center;
                  padding:5px 5px 5px 5px;
                  align-items:center;
                  justify-content:center;
                  letter-spacing:4px;
                  font-weight:bold;
                  font-family:宋体;
                  font-size:24px;
                  opacity:1;
                }
              }
              .transf-title {
                    text-align: center;
                    line-height: 55px;
                    font-weight: bold;
                    font-size: 28px;
                }
             .orderBorder {
                width:100%;
                margin: 0px auto;
            }
            .orderBorder input{
                width:100%
            }
            .ymd {
                padding: 0;
                margin: 0;
                display: flex;
                width: 80%;
                text-align: right;
                align-items: center;
            }
            .pinput {
                display: flex;
                flex-direction: row;
                line-height: 42px;
                align-items: center;
            }
            .el-checkbox{
                display: flex;
                flex-direction: row;
                line-height: 42px;
                align-items: center;
            }
            .el-checkbox .el-checkbox__label{
                margin-left:10px
            }

            .orderBorder input {
                height:30px;
                 border:0!important;

                }
                 .textNSME input {
                        text-align: right !important;
                    }

                input::-webkit-outer-spin-button,
                 input::-webkit-inner-spin-button {
                    -webkit-appearance: none !important;
                }

              input[type="number"] {
                    -moz-appearance: textfield;
                }

                .margin-input{
                    padding-top:10px
                }

            </style>
          </head>
          <body>
             ${tableElement.outerHTML}
            <script>
              window.onload = function() {
                window.print();
                window.close();
              }
            <\/script>
          </body>
          </html>
      `);
            iframeDoc.close();
        },
    }
}
</script>
